package com.cskaoyan.shopping.service.impl;

import com.cskaoyan.mall.commons.exception.ExceptionProcessorUtils;
import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.mall.dto.*;
import com.cskaoyan.shopping.converter.ContentConverter;
import com.cskaoyan.shopping.converter.ProductConverter;
import com.cskaoyan.shopping.dal.entitys.Item;
import com.cskaoyan.shopping.dal.entitys.ItemCat;
import com.cskaoyan.shopping.dal.entitys.Panel;
import com.cskaoyan.shopping.dal.entitys.PanelContentItem;
import com.cskaoyan.shopping.dal.persistence.*;
import com.cskaoyan.shopping.dto.*;
import com.cskaoyan.shopping.service.IProductCateService;
import com.cskaoyan.shopping.service.IProductService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @ClassName: IProductServiceImpl
 * @Description: 商品详情实现类
 * @Author:YPS
 * @Date: 2022/5/19 21:45
 * @Version: 1.0
 **/
@Service
@Transactional
public class IProductServiceImpl implements IProductService {

    @Autowired
    ItemMapper itemMapper;
    @Autowired
    ItemDescMapper itemDescMapper;
    @Autowired
    PanelContentMapper panelContentMapper;
    @Autowired
    ProductConverter productConverter;
    @Autowired
    PanelMapper panelMapper;
    @Autowired
    ContentConverter contentConverter;

    @Override
    public ProductDetailResponse getProductDetail(ProductDetailRequest request) {
        Long id = request.getId();
        System.out.println(id);
        ProductDetailResponse response = new ProductDetailResponse();
        try {
            ProductDetailDto productDetailDto = itemMapper.selectItemByProductId(id);
            productDetailDto.setDetail(itemDescMapper.selectDetailByProductId(id));
            productDetailDto.setProductImageSmall(panelContentMapper.selectProductImageSmallByProductId(id));
            response.setProductDetailDto(productDetailDto);
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            ExceptionProcessorUtils.wrapperHandlerException(response, e);
        }
        return response;
    }

    @Override
    public AllProductResponse getAllProduct(AllProductRequest request) {
        Integer page = request.getPage();//页码
        Integer limit = request.getSize();//数据量
        String sort = null;
        if (request.getSort() != null && request.getSort() != "") {
            Integer i = Integer.parseInt(request.getSort());//排序列 ASC->1 DESC->-1
            switch (i) {
                case 1:
                    sort = "ASC";
                    break;
                case -1:
                    sort = "desc";
                    break;
            }
        }
        //分页插件 PageHelper，辅助我们做分页以及分页信息的获得
        PageHelper.startPage(page, limit);
        List<Item> list = itemMapper.selectItemFront(request.getCid(), "price", sort, request.getPriceGt(), request.getPriceLte());
        List<ProductDto> productDtos = productConverter.items2Dto(list);
        AllProductResponse response = new AllProductResponse();
        response.setProductDtoList(productDtos);
        PageInfo<Item> itemPageInfo = new PageInfo<>(list);
        long total = itemPageInfo.getTotal();
        response.setTotal(total);
        response.setCode(ShoppingRetCode.SUCCESS.getCode());
        response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        return response;
    }

    @Override
    public RecommendResponse getRecommendGoods() {
        RecommendResponse response = new RecommendResponse();
        List<Panel> panels = panelMapper.selectPanelContentById(6);
        Panel panel = panels.get(0);
        PanelDto panelDto = contentConverter.panel2Dto(panel);
        Set<PanelDto> set = new HashSet<>();
        set.add(panelDto);
        response.setPanelContentItemDtos(set);
        response.setCode(ShoppingRetCode.SUCCESS.getCode());
        response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        return response;
    }

    @Override
    public AllItemResponse getAllItems() {
        return null;
    }

    @Override
    /**
     * @author: huzhouming
     * @description: TODO 商品的模糊查询
     * @date: 2022/5/20
     * @param request
     * @return com.cskaoyan.shopping.dto.AllProductResponse
     */
    public AllProductResponse search(SearchProductRequest request) {
        Integer page = request.getPage();//页码
        Integer limit = request.getSize();//数据量
        String sort = null;
        if (request.getSort() != null && request.getSort() != "") {
            Integer i = Integer.parseInt(request.getSort());//排序列 ASC->1 DESC->-1
            switch (i) {
                case 1:
                    sort = "ASC";
                    break;
                case -1:
                    sort = "desc";
                    break;
            }
        }
        //分页插件 PageHelper，辅助我们做分页以及分页信息的获得
        PageHelper.startPage(page, limit);
        List<Item> list = itemMapper.selectItemFrontBySearch(request.getKey(), "price", sort, request.getPriceGt(), request.getPriceLte());
        List<ProductDto> productDtos = productConverter.items2Dto(list);
        AllProductResponse response = new AllProductResponse();
        response.setProductDtoList(productDtos);
        PageInfo<Item> itemPageInfo = new PageInfo<>(list);
        long total = itemPageInfo.getTotal();
        response.setTotal(total);
        response.setCode(ShoppingRetCode.SUCCESS.getCode());
        response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        return response;
    }
}
